package com.bst.bsj.bops.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bst.bsj.base.controller.WebBaseController;
import com.bst.bsj.bops.web.biz.SeedlingNamesConfigAndTypeRelationService;
import com.bst.bsj.bops.web.biz.SeedlingNamesConfigService;
import com.bst.bsj.common.dal.dataobject.SeedlingNamesConfig;
import com.bst.bsj.common.dal.dataobject.SeedlingNamesConfigAndTypeRelation;
import com.bst.bsj.portal.util.PinYin2Abbreviation;
import com.homewaimai.common.lang.Paginator;
import com.homewaimai.common.lang.Result;
import com.homewaimai.common.lang.StringUtil;

@Scope("prototype")
@Controller
@RequestMapping(value = "/bops/sys/sNamesConfig")
public class SeedlingNamesConfigController extends WebBaseController {

  @Autowired
  private SeedlingNamesConfigService seedlingNamesConfigService;

  @Autowired
  private SeedlingNamesConfigAndTypeRelationService andTypeRelationService;

  /**
   * 
  * @Title: auditList
  * @Description: TODO(这里用一句话描述这个方法的作用)
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * ModelAndView
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月14日 上午11:43:29
  * @modificator :tonglu
  * @modify date :2016年5月14日 上午11:43:29
   */
  @SuppressWarnings("unchecked")
  @RequestMapping(value = "/sNamesConfigList")
  public ModelAndView auditList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    ModelAndView modelAndView = getBopsModelAndView("goods/popular_name");

    String baseName = (String) params.get("baseName");//名称
    String commonNames = (String) params.get("commonNames");
    String sstName = (String) params.get("sstName");
    if (baseName != null) {
      baseName = baseName.trim();
    }
    if (commonNames != null) {
      commonNames = commonNames.trim();
    }
    if (sstName != null) {
      sstName = sstName.trim();
    }
    String pageNums = (String) params.get("pageNum");
    Integer pageNum = 0;
    if (StringUtil.isNotBlank(baseName)) {
      paramsMap.put("baseName", baseName);
    }
    if (StringUtil.isNotBlank(commonNames)) {
      paramsMap.put("commonNames", commonNames);
    }
    if (StringUtil.isNotBlank(sstName)) {
      paramsMap.put("sstName", sstName);
    }
    if (StringUtil.isNumeric(pageNums)) {
      pageNum = Integer.valueOf(pageNums);
    }
    if (pageNum == null || pageNum == 0) {
      pageNum = 1;
    }
    paramsMap.put("page", pageNum);
    paramsMap.put("length", new Long(10));
    paramsMap.put("offset", new Long((pageNum - 1) * 10));
    Result result = seedlingNamesConfigService.query(paramsMap);
    // 如果请求成功
    if (result.isSuccess()) {
      // 得到分页器
      Paginator paginator = (Paginator) result.getModels().get("paginator");
      // 得到数据
      List<SeedlingNamesConfig> seedlingNamesConfigs = (List<SeedlingNamesConfig>) result.getModels().get(
          "seedlingNamesConfigs");
      modelAndView.addObject("seedlingNamesConfigs", seedlingNamesConfigs);
      modelAndView.addObject("paginator", paginator);
    }
    modelAndView.addObject("params", params);
    return modelAndView;
  }

  /**
   * 
  * @Title: edit
  * @Description: TODO(编辑)
  * @param req
  * @param resp
  * @param id
  * @return
  * @throws Exception
  * ModelAndView
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月13日 下午7:34:50
  * @modificator :tonglu
  * @modify date :2016年5月13日 下午7:34:50
   */
  @RequestMapping(value = "/edit")
  public ModelAndView edit(HttpServletRequest req, HttpServletResponse resp, String id) throws Exception {
    ModelAndView modelAndView = new ModelAndView();
    SeedlingNamesConfig seedlingNamesConfig = seedlingNamesConfigService.getSeedlingNamesConfig(Long.valueOf(id));
    modelAndView.addObject("seedlingNamesConfig", seedlingNamesConfig);
    return modelAndView;
  }

  @RequestMapping(value = "/save")
  @ResponseBody
  public String save(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    SeedlingNamesConfig namesConfig = new SeedlingNamesConfig();
    String baseName = (String) params.get("baseName");
    String commonNames = (String) params.get("commonNames");
    String searchKeyWords = (String) params.get("searchKeyWords");
    String seedlingTypeNumbers = (String) params.get("seedlingTypeNumbers");
    String[] strs = seedlingTypeNumbers.split(",");
    String id = (String) params.get("id");
    if (baseName != null) {
      baseName = baseName.trim();
    }
    if (commonNames != null) {
      commonNames = commonNames.trim();
    }
    if (searchKeyWords != null) {
      searchKeyWords = searchKeyWords.trim();
    }
    Long andTypeRelationId = 0L;
    if (id != null) {
      namesConfig = seedlingNamesConfigService.getSeedlingNamesConfig(Long.valueOf(id));
    }
    namesConfig.setBaseName(baseName);
    namesConfig.setCommonNames(commonNames);
    namesConfig.setSearchKeyWords(searchKeyWords);
    namesConfig.setSeedlingTypeNumbers(seedlingTypeNumbers);
    
    if(StringUtil.isNotEmpty(baseName)) {
      Character ch=baseName.charAt(0);
      namesConfig.setBeginLetter(new PinYin2Abbreviation().Char2Initial(ch)+"");
    }
    
    if (id != null) {
      andTypeRelationId = Long.valueOf(id);
      seedlingNamesConfigService.update(namesConfig);
    } else {
      andTypeRelationId = seedlingNamesConfigService.insert(namesConfig);
    }
    andTypeRelationService.delete(andTypeRelationId);
    if (!"".equals(seedlingTypeNumbers)) {
      for (String string : strs) {
        SeedlingNamesConfigAndTypeRelation andTypeRelation = new SeedlingNamesConfigAndTypeRelation();
        andTypeRelation.setCommonNameNumber(andTypeRelationId + "");
        andTypeRelation.setSeedlingTypeNumber(string);
        andTypeRelationService.insert(andTypeRelation);
      }
    }
    return "success";
  }

  @RequestMapping(value = "/delete")
  public void delete(HttpServletRequest req, HttpServletResponse resp, String id) throws Exception {
    seedlingNamesConfigService.delete(Long.valueOf(id));
    andTypeRelationService.delete(Long.valueOf(id));
  }
}
